package com.btex.app.pager;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;

import com.btex.app.tools.SharedPreferencesUtil;
import com.umeng.analytics.MobclickAgent;

import java.util.Locale;

/**
 * Created by wcx on 16/3/16.
 */
public abstract class BaseFragment extends Fragment {
    protected Context cxt;
    protected SharedPreferencesUtil sp;
    protected String token;
    protected View view;
    private Resources mResources;
    private Configuration mConfiguration;
    private DisplayMetrics mDisplayMetrics;
    private boolean mIsEngilsh;
    private String mCity_new;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        cxt = getActivity();
        sp = SharedPreferencesUtil.getSharedPreferences(cxt);

    }


    public abstract void setData();

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(cxt);
        mResources = getResources();
        mConfiguration = mResources.getConfiguration();
        mDisplayMetrics = mResources.getDisplayMetrics();

        mIsEngilsh = sp.getBoolean("isEngilsh", false);
        mCity_new = sp.getString("city_new", "0");


        if (mCity_new.equals("0")) {
//            中文
            Log.i("BaseFragment", "0");
            mConfiguration.locale = Locale.SIMPLIFIED_CHINESE;
            mResources.updateConfiguration(mConfiguration, mDisplayMetrics);

        } else if (mCity_new.equals("1")) {
//            英文
            Log.i("BaseFragment", "1");

//            mConfiguration.locale = Locale.US;
            Locale spanish = new Locale("en");
            mConfiguration.locale = spanish;
            mResources.updateConfiguration(mConfiguration, mDisplayMetrics);

        } else if (mCity_new.equals("2")) {
//            日语
            Log.i("BaseFragment", "2");
            mConfiguration.locale = Locale.JAPANESE;
            mResources.updateConfiguration(mConfiguration, mDisplayMetrics);
        } else {
            Log.i("BaseFragment", "3");
//            西班牙
            Locale spanish = new Locale("es", "rES");
            mConfiguration.locale = spanish;
            mResources.updateConfiguration(mConfiguration, mDisplayMetrics);
        }


//
//        if (!mIsEngilsh) {
//
//            mConfiguration.locale = Locale.SIMPLIFIED_CHINESE;
//            mResources.updateConfiguration(mConfiguration, mDisplayMetrics);
//        } else {
//
////            mConfiguration.locale = Locale.US;
//            mConfiguration.locale = Locale.JAPAN;
//            mResources.updateConfiguration(mConfiguration, mDisplayMetrics);
//        }


    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(cxt);
    }
}
